package cn.tedu.mall.seckill.controller;

import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.pojo.seckill.dto.SeckillOrderAddDTO;
import cn.tedu.mall.pojo.seckill.vo.SeckillCommitVO;
import cn.tedu.mall.seckill.service.ISeckillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @author zhangyiping
 * @date 2024/1/23
 */
@RestController
@RequestMapping("/seckill")
@Api(tags = "订单秒杀功能之秒杀下单")
public class SeckillCommitController {

    @Autowired
    private ISeckillService seckillService;

    @PostMapping("/{randCode}")
    @ApiOperation(value = "秒杀下单")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult<SeckillCommitVO> commit(@PathVariable("randCode") String randCode, SeckillOrderAddDTO seckillOrderAddDTO) {
        // sn没有从前端传递，则人为设置一个简单的sn
        String sn = UUID.randomUUID().toString();
        seckillOrderAddDTO.setSn(sn);
        //路径参数封装到dto对象，交给业务层处理对比逻辑
        seckillOrderAddDTO.setRandCode(randCode);
        SeckillCommitVO seckillCommitVO = seckillService.commitSeckill(seckillOrderAddDTO);
        return JsonResult.ok(seckillCommitVO);
    }

}
